package env

import (
	"os"
	"strings"
	"kumachan/standalone/rx"
	"kumachan/interpreter/runtime/def"
)


func NewDefaultEnvironment(args ([] string)) *def.Environment {
	return &def.Environment {
		Stdin:   rx.FileFrom(os.Stdin),
		Stdout:  rx.FileFrom(os.Stdout),
		Stderr:  rx.FileFrom(os.Stderr),
		Args:    CloneArgs(args),
		EnvVars: AdaptEnvVars(os.Environ()),
	}
}

func CloneArgs(args ([] string)) ([] string) {
	var args_clone = make([] string, len(args))
	copy(args_clone, args)
	return args_clone
}

func AdaptEnvVars(raw ([] string)) (map[string] string) {
	var m = make(map[string] string)
	for _, item := range raw {
		var n = strings.IndexRune(item, '=')
		if n == -1 {
			m[item] = ""
		} else {
			m[item[:n]] = item[(n + 1):]
		}
	}
	return m
}


